<template>
  <div class="goodDetail">
    <div class="top">
      <div class="back-icon-box" @click="$router.back()">
        <van-icon class="icon" name="arrow-left" />
      </div>
      <van-swipe :autoplay="3000">
        <van-swipe-item v-for="item in detail.pics" :key="item.id">
          <img :src="item.pic" />
        </van-swipe-item>
      </van-swipe>
      <div class="top-detail">
        <h3>{{ baseInfo.name }}</h3>
        <span class="desc">{{ baseInfo.characteristic }}</span>
        <section>
          <span class="minPrice"
            >底价<em>￥{{ baseInfo.minPrice }}</em
            ><i>原价￥{{ baseInfo.originalPrice }}</i></span
          >
          <span>库存{{ baseInfo.stores }}</span>
        </section>
      </div>
    </div>
    <my-good-detail :content="detail.content" :reps="reps"></my-good-detail>
    <my-buy @changeshow="changeShow" v-if="$route.query.type !== 'kj'"></my-buy>
    <div class="bargin" v-else>立即发起砍价，最低可砍到1元</div>
    <van-popup v-model:show="show" position="bottom">
      <div class="add-cart-top">
        <img class="pop-img" :src="baseInfo.pic" alt="" />
        <section>
          <h3>{{ baseInfo.name }}</h3>
          <span>￥{{ baseInfo.minPrice }}</span>
        </section>
        <van-icon @click="show = false" class="close" name="cross" />
      </div>
      <div>
        <div v-for="(s, index) in size" :key="index" class="size-box">
          <h3>{{ s.name }}</h3>
          <ul>
            <li
              @click="choseLi(index, ind, item)"
              :class="'li' + index"
              v-for="(item, ind) in s.childsCurGoods"
              :key="item.id"
            >
              {{ item.name }}
            </li>
          </ul>
        </div>
      </div>
      <div class="buy-count">
        <span>购买数量</span>
        <van-stepper v-model="value" />
      </div>
      <div class="add-btn" @click="addProducts">加入购物车</div>
    </van-popup>
  </div>
</template>

<script>
import { goodDetail, getRep } from "@/api/home";
import { Toast } from "vant";
import myGoodDetail from "@/components/myGoodDetail/myGoodDetail";
import myBuy from "@/components/myBuy/myBuy";
import store from "@/utils/store";
export default {
  name: "",
  data() {
    return {
      detail: {},
      baseInfo: {},
      reps: [],
      show: false,
      value: 1,
      size: [],
      cur: null,
      cur2: null,
      s: {
        up: "",
        upId: "",
      },
      b: {
        btm: "",
        btmId: "",
      },
    };
  },
  components: {
    myGoodDetail,
    myBuy,
  },
  created() {
    // 请求商品详情信息
    goodDetail(this.$route.params.id).then((res) => {
      this.detail = res.data;
      this.baseInfo = res.data.basicInfo;
      this.size = res.data.properties;
    });

    // 请求商品评价信息
    getRep(this.$route.params.id).then((res) => {
      // console.log(res);
      this.reps = res.data;
    });
  },

  methods: {
    changeShow(bool) {
      this.show = bool;
    },
    // 选择规格的事件
    choseLi(i, ind, item) {
      let className = ".li" + i;
      let lis = document.querySelectorAll(className);
      for (let j = 0; j < lis.length; j++) {
        lis[j].style.borderColor = "#ccc";
      }
      lis[ind].style.borderColor = "red";
      if (i === 0) {
        this.cur = 0;
        this.s.up = item.name;
        this.s.upId = item.id;
      } else if (i === 1) {
        this.cur2 = 1;
        this.b.btm = item.name;
        this.b.btmId = item.id;
      }
    },

    addProducts() {
      if (this.size.length === 1) {
        if (this.cur === null) {
          Toast.fail("请选择规格");
          return;
        }
      } else if (this.size.length === 2) {
        if (this.cur === null || this.cur2 === null) {
          Toast.fail("请选择规格");
          return;
        }
      }
      let obj = {
        price: this.baseInfo.minPrice, // 商品价格
        title: this.baseInfo.name, // 商品名称
        src: this.baseInfo.pic, // 商品对应图片
        num: this.value, // 商品数量
        up: this.s.up, // 规格中选中的尺寸
        upId: this.s.upId, // 选中的尺寸id
        check: false, // 定义商品是否被选中的字段
        btm: this.b.btm, // 规格中的颜色
        btmId: this.b.btmId, // 选中的颜色id
      };
      // console.log(store);
      store.addActions(obj);
      Toast.fail("加入成功");
      this.show = false;
    },
  },
};
</script>

<style scoped lang="scss">
.goodDetail {
  height: 100%;
  overflow: auto;
  padding-bottom: 100px;
}
img {
  width: 100%;
  height: 750px;
}
.top {
  margin-bottom: 30px;
  position: relative;
}
.back-icon-box {
  position: absolute;
  top: 20px;
  left: 20px;
  padding: 6px;
  border: 1px solid #fff;
  border-radius: 50%;
  z-index: 5;
  color: #666;
  .icon {
    background-color: #fff;
    border-radius: 50%;
  }
}
.top-detail {
  padding-top: 40px;
  padding-left: 30px;
  padding-bottom: 20px;
  background-color: #fff;
  h3 {
    font-size: 36px;
    color: #333;
  }
  .desc {
    font-size: 24px;
    color: #b2b2b2;
  }
  section {
    display: flex;
    width: 100%;
    justify-content: space-between;
    font-size: 24px;
    margin-top: 40px;
    padding-right: 20px;
    color: #b2b2b2;
    .minPrice {
      color: #cc0c0c;
      em {
        font-size: 30px;
        margin-right: 40px;
      }
      i {
        color: #b2b2b2;
      }
    }
  }
}
.bargin {
  background-color: #cc0c0c;
  color: #fff;
  text-align: center;
  width: 100%;
  height: 100px;
  line-height: 100px;
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 10;
  font-size: 26px;
}
.pop-img {
  width: 126px;
  height: 126px;
}
.add-cart-top {
  display: flex;
  font-size: 26px;
  justify-content: space-between;
  padding: 30px 20px;
  border-bottom: 1px solid #ccc;
  section {
    margin-left: 30px;
    display: flex;
    flex-direction: column;
    width: calc(100% - 150px);
    justify-content: space-around;
  }
  .close {
    font-size: 40px;
  }
}
.size-box {
  padding: 30px 20px;
  border-bottom: 1px solid #ccc;
  font-size: 26px;
  h3 {
    margin-bottom: 30px;
  }
  ul {
    display: flex;
    flex-wrap: wrap;
    li {
      padding: 14px;
      border: 1px solid #ccc;
      border-radius: 8px;
      margin-right: 20px;
      margin-top: 20px;
    }
    .active {
      border-color: #b7282e;
    }
  }
}
.buy-count {
  display: flex;
  justify-content: space-between;
  height: 136px;
  align-items: center;
  font-size: 26px;
  padding: 0 30px;
}
.add-btn {
  background-color: #b7282e;
  width: 100%;
  text-align: center;
  line-height: 80px;
  color: #fff;
  font-size: 30px;
}
</style>
